package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopCollectBO;
import com.ztgf.shopping.models.bo.shop.ShopCollectBatchSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopCollectSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopCollectDTO;
import com.ztgf.shopping.models.service.shop.IShopCollectService;
import com.ztgf.shopping.models.vo.shop.ShopCollectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 用户收藏表对外接口类
 *
 * @author zhanghualang
 * @Date 2020-05-26
 */

@RestController
@RequestMapping("/api/shopCollect")
@Slf4j
@Api(value = "ShopCollectController",tags = "用户收藏表接口")
public class ShopCollectApiController {
    @DubboReference
    private IShopCollectService iShopCollectService;

    @ApiOperation(value = "添加用户收藏表",notes = "添加用户收藏表")
    @PostMapping(value = "/insert")
    public Result insert( ShopCollectSaveBO insertSaveBO) {
        if (iShopCollectService.updateByShop(insertSaveBO)){
            return new Result<>(CodeEnum.FAIL_100035);
        }

        boolean flaga = iShopCollectService.add(insertSaveBO);
        if (flaga){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "批量添加用户收藏表",notes = "批量添加用户收藏表")
    @PostMapping(value = "/insertBatch")
    public Result insertBatch(ShopCollectBatchSaveBO insertSaveBO) {

        boolean flag = iShopCollectService.addBatch(insertSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "判断用户是否已经收藏",notes = "判断用户是否已经收藏")
    @PostMapping(value = "/getCollect")
    public Result getCollect( ShopCollectSaveBO insertSaveBO) {
        boolean flag = iShopCollectService.updateByShop(insertSaveBO);
        if (flag){
            return new Result<>(CodeEnum.FAIL_100035);
        }else {
            return new Result<>();
        }
    }

    @ApiOperation(value = "删除用户收藏表",notes = "删除用户收藏表")
    @ApiImplicitParam(name = "id",value = "用户收藏表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = iShopCollectService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "点铺首页，商品详情页，取消收藏",notes = "点铺首页，商品详情页，取消收藏")
    @ApiImplicitParam(name = "id",value = "用户收藏表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/deleteBo")
    public Result delete( ShopCollectSaveBO bo){

        boolean flag = iShopCollectService.deleteByBo(bo);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除用户收藏表",notes = "批量删除用户收藏表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        iShopCollectService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找用户收藏表",notes = "根据id查找用户收藏表")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopCollectDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        ShopCollectDTO shopCollectDTO = iShopCollectService.selectById(id);
        return new Result<>(shopCollectDTO);
    }

    @ApiOperation(value = "用户收藏表列表",notes = "用户收藏表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopCollectVO>> queryPageList(ShopCollectBO shopCollectBO){
        Page<ShopCollectVO> page = iShopCollectService.selectPageVO(shopCollectBO);

        return new Result<>(page);
    }

    @PostMapping("/getBeCollectCommodityCount")
    @ApiOperation(value = "根据商品id获取被收藏数",notes = "根据商品id获取被收藏数")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<Integer> getBeCollectCommodityCount(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Integer commodityCount = iShopCollectService.getBeCollectCommodityCount(id);
        return new Result<>(commodityCount);
    }

    @PostMapping("/getBeCollectShopCount")
    @ApiOperation(value = "根据店铺id获取被收藏数",notes = "根据店铺id获取被收藏数")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<Integer> getBeCollectShopCount(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Integer commodityCount = iShopCollectService.getBeCollectShopCount(id);
        return new Result<>(commodityCount);
    }

    @PostMapping("/getCountByShopid")
    @ApiOperation(value = "根据店铺id获取收藏数",notes = "根据店铺id获取收藏数")
    public Result<Integer> getCountByShopid(Long collectShopId, Integer type){
        if(collectShopId == null || type == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("collect_shop_id", collectShopId).eq("type", type);
        Integer shopCount = iShopCollectService.selectCount(wrapper);
        return new Result<>(shopCount);
    }
}
